home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / snmp-dev.000 / snmp-dev / snmp_api.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-09  |  9.0 KB  |  261 lines

  1. /***********************************************************
  2.     Copyright 1989 by Carnegie Mellon University
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of CMU not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23.  * snmp_api.h - API for access to snmp.
  24.  */
  25.  
  26.  
  27. typedef struct sockaddr_in  ipaddr;
  28.  
  29. struct snmp_session {
  30.     u_char  *community;    /* community for outgoing requests. */
  31.     int        community_len;  /* Length of community name. */
  32.     int        retries;    /* Number of retries before timeout. */
  33.     long    timeout;    /* Number of uS until first timeout, then exponential backoff */
  34.     char    *peername;    /* Domain name or dotted IP address of default peer */
  35.     u_short remote_port;/* UDP port number of peer. */
  36.     u_short local_port; /* My UDP port number, 0 for default, picked randomly */
  37.     /* Authentication function or NULL if null authentication is used */
  38.     u_char    *(*authenticator)();
  39.     int        (*callback)();  /* Function to interpret incoming data */
  40.     /* Pointer to data that the callback function may consider important */
  41.     void    *callback_magic;
  42.     int        version;
  43.     oid        *srcParty;
  44.     int        srcPartyLen;
  45.     oid        *dstParty;
  46.     int        dstPartyLen;
  47.     oid        *context;
  48.     int        contextLen;
  49. };
  50.  
  51. /*
  52.  * Set fields in session and pdu to the following to get a default or unconfigured value.
  53.  */
  54. #define SNMP_DEFAULT_COMMUNITY_LEN  0    /* to get a default community name */
  55. #define SNMP_DEFAULT_RETRIES        -1
  56. #define SNMP_DEFAULT_TIMEOUT        -1
  57. #define SNMP_DEFAULT_REMPORT        0
  58. #define SNMP_DEFAULT_REQID        0
  59. #define SNMP_DEFAULT_ERRSTAT        -1
  60. #define SNMP_DEFAULT_ERRINDEX        -1
  61. #define SNMP_DEFAULT_ADDRESS        0
  62. #define SNMP_DEFAULT_PEERNAME        NULL
  63. #define SNMP_DEFAULT_ENTERPRISE_LENGTH    0
  64. #define SNMP_DEFAULT_TIME        0
  65. #define SNMP_DEFAULT_VERSION        -1
  66.  
  67. extern int snmp_errno;
  68. /* Error return values */
  69. #define SNMPERR_GENERR        -1
  70. #define SNMPERR_BAD_LOCPORT    -2  /* local port was already in use */
  71. #define SNMPERR_BAD_ADDRESS    -3
  72. #define SNMPERR_BAD_SESSION    -4
  73. #define SNMPERR_TOO_LONG    -5
  74.  
  75. #define non_repeaters    errstat
  76. #define max_repetitions errindex
  77.  
  78. struct snmp_pdu {
  79.     int        version;
  80.  
  81.     ipaddr  address;    /* Address of peer */
  82.     oid        *srcParty;
  83.     int        srcPartyLen;
  84.     oid        *dstParty;
  85.     int        dstPartyLen;
  86.     oid        *context;
  87.     int     contextLen;
  88.  
  89.     u_char  *community;    /* community for outgoing requests. */
  90.     int        community_len;  /* Length of community name. */
  91.  
  92.     int        command;    /* Type of this PDU */
  93.  
  94.     long  reqid;    /* Request id */
  95.     long  errstat;    /* Error status (non_repeaters in GetBulk) */
  96.     long  errindex;    /* Error index (max_repetitions in GetBulk) */
  97.  
  98.     /* Trap information */
  99.     oid        *enterprise;/* System OID */
  100.     int        enterprise_length;
  101.     ipaddr  agent_addr;    /* address of object generating trap */
  102.     int        trap_type;    /* trap type */
  103.     int        specific_type;  /* specific type */
  104.     u_long  time;    /* Uptime */
  105.  
  106.     struct variable_list *variables;
  107. };
  108.  
  109.  
  110. struct variable_list {
  111.     struct variable_list *next_variable;    /* NULL for last variable */
  112.     oid        *name;  /* Object identifier of variable */
  113.     int        name_length;    /* number of subid's in name */
  114.     u_char  type;   /* ASN type of variable */
  115.     union { /* value of variable */
  116.     long    *integer;
  117.     u_char    *string;
  118.     oid    *objid;
  119.     u_char  *bitstring;
  120.     struct counter64 *counter64;
  121.     } val;
  122.     int        val_len;
  123. };
  124.  
  125. /*
  126.  * struct snmp_session *snmp_open(session)
  127.  *    struct snmp_session *session;
  128.  * 
  129.  * Sets up the session with the snmp_session information provided
  130.  * by the user.  Then opens and binds the necessary UDP port.
  131.  * A handle to the created session is returned (this is different than
  132.  * the pointer passed to snmp_open()).  On any error, NULL is returned
  133.  * and snmp_errno is set to the appropriate error code.
  134.  */
  135. struct snmp_session *snmp_open();
  136.  
  137. /*
  138.  * int snmp_close(session)
  139.  *     struct snmp_session *session;
  140.  * 
  141.  * Close the input session.  Frees all data allocated for the session,
  142.  * dequeues any pending requests, and closes any sockets allocated for
  143.  * the session.  Returns 0 on error, 1 otherwise.
  144.  */
  145. int snmp_close();
  146.  
  147.  
  148. /*
  149.  * int snmp_send(session, pdu)
  150.  *     struct snmp_session *session;
  151.  *     struct snmp_pdu    *pdu;
  152.  * 
  153.  * Sends the input pdu on the session after calling snmp_build to create
  154.  * a serialized packet.  If necessary, set some of the pdu data from the
  155.  * session defaults.  Add a request corresponding to this pdu to the list
  156.  * of outstanding requests on this session, then send the pdu.
  157.  * Returns the request id of the generated packet if applicable, otherwise 1.
  158.  * On any error, 0 is returned.
  159.  * The pdu is freed by snmp_send() unless a failure occured.
  160.  */
  161. int snmp_send();
  162.  
  163.  
  164. /*
  165.  * void snmp_read(fdset)
  166.  *     fd_set  *fdset;
  167.  * 
  168.  * Checks to see if any of the fd's set in the fdset belong to
  169.  * snmp.  Each socket with it's fd set has a packet read from it
  170.  * and snmp_parse is called on the packet received.  The resulting pdu
  171.  * is passed to the callback routine for that session.  If the callback
  172.  * routine returns successfully, the pdu and it's request are deleted.
  173.  */
  174. void snmp_read();
  175.  
  176.  
  177. /*
  178.  * void
  179.  * snmp_free_pdu(pdu)
  180.  *     struct snmp_pdu *pdu;
  181.  * 
  182.  * Frees the pdu and any malloc'd data associated with it.
  183.  */
  184. void snmp_free_pdu();
  185.  
  186. /*
  187.  * int snmp_select_info(numfds, fdset, timeout, block)
  188.  * int *numfds;
  189.  * fd_set   *fdset;
  190.  * struct timeval *timeout;
  191.  * int *block;
  192.  *
  193.  * Returns info about what snmp requires from a select statement.
  194.  * numfds is the number of fds in the list that are significant.
  195.  * All file descriptors opened for SNMP are OR'd into the fdset.
  196.  * If activity occurs on any of these file descriptors, snmp_read
  197.  * should be called with that file descriptor set.
  198.  *
  199.  * The timeout is the latest time that SNMP can wait for a timeout.  The
  200.  * select should be done with the minimum time between timeout and any other
  201.  * timeouts necessary.  This should be checked upon each invocation of select.
  202.  * If a timeout is received, snmp_timeout should be called to check if the
  203.  * timeout was for SNMP.  (snmp_timeout is idempotent)
  204.  *
  205.  * Block is 1 if the select is requested to block indefinitely, rather than time out.
  206.  * If block is input as 1, the timeout value will be treated as undefined, but it must
  207.  * be available for setting in snmp_select_info.  On return, if block is true, the value
  208.  * of timeout will be undefined.
  209.  *
  210.  * snmp_select_info returns the number of open sockets.  (i.e. The number of sessions open)
  211.  */
  212. int snmp_select_info();
  213.  
  214. /*
  215.  * void snmp_timeout();
  216.  * 
  217.  * snmp_timeout should be called whenever the timeout from snmp_select_info expires,
  218.  * but it is idempotent, so snmp_timeout can be polled (probably a cpu expensive
  219.  * proposition).  snmp_timeout checks to see if any of the sessions have an
  220.  * outstanding request that has timed out.  If it finds one (or more), and that
  221.  * pdu has more retries available, a new packet is formed from the pdu and is
  222.  * resent.  If there are no more retries available, the callback for the session
  223.  * is used to alert the user of the timeout.
  224.  */
  225. void snmp_timeout();
  226.  
  227.  
  228. /*
  229.  * This routine must be supplied by the application:
  230.  *
  231.  * u_char *authenticator(pdu, length, community, community_len)
  232.  * u_char *pdu;        The rest of the PDU to be authenticated
  233.  * int *length;        The length of the PDU (updated by the authenticator)
  234.  * u_char *community;    The community name to authenticate under.
  235.  * int    community_len    The length of the community name.
  236.  *
  237.  * Returns the authenticated pdu, or NULL if authentication failed.
  238.  * If null authentication is used, the authenticator in snmp_session can be
  239.  * set to NULL(0).
  240.  */
  241.  
  242. /*
  243.  * This routine must be supplied by the application:
  244.  *
  245.  * int callback(operation, session, reqid, pdu, magic)
  246.  * int operation;
  247.  * struct snmp_session *session;    The session authenticated under.
  248.  * int reqid;                The request id of this pdu (0 for TRAP)
  249.  * struct snmp_pdu *pdu;        The pdu information.
  250.  * void *magic                A link to the data for this routine.
  251.  *
  252.  * Returns 1 if request was successful, 0 if it should be kept pending.
  253.  * Any data in the pdu must be copied because it will be freed elsewhere.
  254.  * Operations are defined below:
  255.  */
  256. #define RECEIVED_MESSAGE   1
  257. #define TIMED_OUT       2
  258.  
  259.  
  260. extern int snmp_dump_packet;
  261.